<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-button v-if="sales_intention_flowup_add" class="filter-item" style="margin-left: 10px;" @click="handleCreate"
                 type="primary" icon="edit">添加
      </el-button>
    </div>
    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit
              highlight-current-row style="width: 100%">
      <el-table-column align="center" label="" v-if="dialogStatus == 'update'">
        <template scope="scope">
          <span>{{ scope.row.fid }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="客户姓名">
        <template scope="scope">
          <span>{{ scope.row.studentId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="意向等级">
        <template scope="scope">
          <span>{{ getLabelByValue(scope.row.intentionLevel) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="备注">
        <template scope="scope">
          <span>{{ scope.row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态">
        <template scope="scope">
          <span>{{ scope.row.bizStatus | statusFilter }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column align="center" label="部门">-->
        <!--<template scope="scope">-->
          <!--<span>{{ scope.row.deptId }}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->

      <!--<el-table-column label="操作" width="160" v-if="sales_intention_flowup_upd || sales_intention_flowup_del">-->
        <!--<template scope="scope">-->
          <!--<el-button v-if="sales_intention_flowup_upd" size="mini" type="success"-->
                     <!--@click="handleUpdate(scope.row)">编辑-->
          <!--</el-button>-->
          <!--<el-button v-if="sales_intention_flowup_del" size="mini" type="danger"-->
                     <!--@click="handleDelete(scope.row)">删除-->
          <!--</el-button>-->
        <!--</template>-->
      <!--</el-table-column>-->
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.page"
                     :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" @close="cancel('form')">
      <el-form :model="form" :rules="rules" ref="form" size="mini" label-width="100px">
        <el-form-item label="编码" prop="fid" v-if="dialogStatus == 'update'">
          <el-input v-model="form.fid" placeholder="" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="意向等级" prop="intentionLevel">
          <el-select v-model="form.intentionLevel" :loading="levelLoading" placeholder="意向等级">
            <el-option v-for="option in levelOptions" :label="option.label" :value="option.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="备注"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="bizStatus">
          <el-input v-model="form.bizStatus" placeholder="状态"></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="deptId">
          <el-input v-model="form.deptId" placeholder="部门"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
        <el-button v-else type="primary" @click="update('form')">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {getListByStudent, addObj, putObj, delObj} from '@/api/sales/sales_intention_flowup'
  import {loadByType} from '@/api/dict'
  import {findByvalue} from '@/util/util.js'
  import waves from '@/directive/waves/index.js' // 水波纹指令
  import { getUserDeptPath} from '@/api/user'
  import {mapGetters} from 'vuex'

  export default {
    name: 'table_sales_intention_flowup',
    directives: {
      waves
    },
    data() {
      return {
        list: null,
        total: null,
        listLoading: true,
        levelLoading: false,
        listQuery: {
          page: 1,
          limit: 20
        },
        rules: {
          fid: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          createUser: [
            {required: true, message: '请输入跟进人', trigger: 'blur'}
          ],
          createTime: [
            {required: true, message: '请输入跟进时间', trigger: 'blur'}
          ],
          updateTime: [
            {required: true, message: '请输入修改时间', trigger: 'blur'}
          ],
          intentionLevel: [
            {required: true, message: '请输入意向等级', trigger: 'blur'}
          ],
          remark: [
            {required: true, message: '请输入备注', trigger: 'blur'}
          ],
          bizStatus: [
            {required: true, message: '请输入状态', trigger: 'blur'}
          ],
          deptId: [
            {required: true, message: '请输入部门', trigger: 'blur'}
          ],
          delFlag: [
            {required: true, message: '请输入删除标识', trigger: 'blur'}
          ]
        },
        form: {
          fid: '',
          createUser: '',
          createTime: '',
          updateTime: '',
          intentionLevel: '',
          remark: '',
          bizStatus: '',
          deptId: '',
          delFlag: ''
        },
        dialogFormVisible: false,
        dialogStatus: '',
        sales_intention_flowup_add: false,
        sales_intention_flowup_upd: false,
        sales_intention_flowup_del: false,
        textMap: {
          update: '编辑',
          create: '创建'
        },
        tableKey: 0,
        levelOptions: []
      }
    },
    computed: {
      ...mapGetters([
        'permissions'
      ])
    },
    filters: {/**
       * 获取用户组织路径
       * @param userId 用户ID
       */
      getUserDeptPath(userId) {
        getUserDeptPath(userId).then(response => {
          this.defaultUser = response.data
        })
      },
      statusFilter(status) {
        const statusMap = {
          1: '有效',
          0: '无效'
        }
        return statusMap[status]
      },
      levelFilter(intentionLevel) {
        return intentionLevel
      }
    },
    created() {
      this.getList()
      this.loadLevelMethod('intention_level')
      this.sales_intention_flowup_add = this.permissions['sales_intention_flowup_add']
      this.sales_intention_flowup_upd = this.permissions['sales_intention_flowup_upd']
      this.sales_intention_flowup_del = this.permissions['sales_intention_flowup_del']
    },
    methods: {
      /**
       * 加载意向等级
       */
      loadLevelMethod(type) {
        loadByType(type).then(response => {
          this.levelOptions = response.data
        })
      },
      getLabelByValue(value) {
        if (value === null) {
          return '-'
        } else {
          return findByvalue(this.levelOptions, value + '')
        }
      },
      getList() {
        this.listLoading = true
        getListByStudent(this.listQuery).then(response => {
          this.list = response.data.records
          this.total = response.data.total
          this.listLoading = false
        })
      },
      handleSizeChange(val) {
        this.listQuery.limit = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      },
      handleDelete(row) {
        delObj(row)
          .then(response => {
            this.dialogFormVisible = false
            this.getList()
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          })
      },
      handleUpdate(row) {
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.form.fid = row.fid
        this.form.createUser = row.createUser
        this.form.createTime = row.createTime
        this.form.updateTime = row.updateTime
        this.form.intentionLevel = row.intentionLevel
        this.form.remark = row.remark
        this.form.bizStatus = row.bizStatus
        this.form.deptId = row.deptId
        this.form.delFlag = row.delFlag
      },
      handleCreate() {
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
      },
      create(formName) {
        const set = this.$refs
        set[formName].validate(valid => {
          if (valid) {
            addObj(this.form)
              .then(() => {
                this.dialogFormVisible = false
                this.getList()
                this.$notify({
                  title: '成功',
                  message: '创建成功',
                  type: 'success',
                  duration: 2000
                })
                const set = this.$refs
                set[formName].resetFields()
              })
          } else {
            return false
          }
        })
      },
      cancel(formName) {
        this.dialogFormVisible = false
        const set = this.$refs
        set[formName].resetFields()
      },
      update(formName) {
        const set = this.$refs
        set[formName].validate(valid => {
          if (valid) {
            this.dialogFormVisible = false
            this.form.password = undefined
            putObj(this.form).then(() => {
              this.dialogFormVisible = false
              this.getList()
              const set = this.$refs
              set[formName].resetFields()
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              })
            })
          } else {
            return false
          }
        })
      }
    }
  }
</script>
